<?php
// +----------------------------------------------------------------------
// | ScrollPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2010 Toojiong All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LIZHEN
// +----------------------------------------------------------------------
// | $Id: Build.func.php 2766 14-5-14 下午4:26 LIZHEN $
// +----------------------------------------------------------------------
// | Package

defined('FRAMEWORK_PATH') or exit();

if(!is_dir(CACHE_PATH) || !is_dir(MODULES_PATH)){
    // 检查缓存目录
    check_dir();
}

// 检查应用目录 如果不存在则自动创建
function check_dir() {

    if(!is_dir(CACHE_PATH))   mkdir(CACHE_PATH);
    _build_app_dir();
}
/**
 * 创建应用目录结构
 */
function _build_app_dir()
{
    if(!is_dir(APP_PATH)) mkdir(APP_PATH,0755,true);
    if(is_writeable(APP_PATH)) {
    $dirs  = array(
        CONFIGS_PATH.'/',
        MODULES_PATH.'/',
        CACHE_PATH.'/',
        MODULES_PATH . '/Controllers/',
        MODULES_PATH . '/Models/',
        MODULES_PATH . '/Views/',
        MODULES_PATH . '/Helpers/',
        );
    foreach ($dirs as $dir){
        if(!is_dir($dir))  mkdir($dir,0755,true);
    }
    build_dir_secure($dirs);
    build_first_action();
}

// 创建测试Action
function build_first_action() {
    $file   =   MODULES_PATH.'/Controllers/IndexController.php';
    if(!is_file($file)){
            $content = '<?php
class IndexController extends Controller {

    public function index() {
        $this->_show("Hello World!");
    }';
        file_put_contents($file,$content);
    }
}

/**
 * 生成目录安全文件
 */
function build_dir_secure($dirs = array()) 
{
    // 目录安全写入（默认开启）
    defined('BUILD_DIR_SECURE')  or define('BUILD_DIR_SECURE',    true);
    if(BUILD_DIR_SECURE) {
        defined('DIR_SECURE_FILENAME')  or define('DIR_SECURE_FILENAME',    'index.html');
        defined('DIR_SECURE_CONTENT')   or define('DIR_SECURE_CONTENT',     ' ');
        // 自动写入目录安全文件
        $content = DIR_SECURE_CONTENT;
        $files = explode(',', DIR_SECURE_FILENAME);
        foreach ($files as $filename){
            foreach ($dirs as $dir)
                file_put_contents($dir.$filename,$content);
        }
    }
}


